<script setup lang="ts">
import { onMounted, watch, inject, ref, type Ref } from 'vue'

const screenSizeLevel = inject("screenSizeLevel")
const isLoginSuccess = inject("isLoginSuccess")
const sidebar = ref(null) as Ref<HTMLElement | null>;
const sidebarLayout = ref('1 /span 2');
const emit = defineEmits(["loginlogout"])


onMounted(function() {
  let sidebarArrow = document.querySelectorAll('.arrow')
  for (var i = 0; i < sidebarArrow.length; i++) {
    sidebarArrow[i].addEventListener('click', (e) => {
      let arrowParent = e.target.parentElement.parentElement
      arrowParent.classList.toggle('showMenu')
    })
  }
})
watch(screenSizeLevel, (value) => {
  if (value === 1 || value === 2 ) {
    sidebar.value.classList.add('close')
    sidebarLayout.value = '1 /span 1'
  } else {
    sidebar.value.classList.remove('close')
    sidebarLayout.value = '1 /span 2'
  }
})
</script>

<template>
  <div class="sidebar__relative__root">
    <div class="sidebar" ref="sidebar">
      <div class="logo-details">
        <i class='bx bx-ghost'></i>
        <span class="project__name">SnowCrystal</span>
      </div>
      <ul class="nav-links">
        <li>
          <router-link to="/">
            <i class='bx bx-home-heart'></i>
            <span class="link_name">首页</span>
          </router-link>
<!--          <ul class="sub-menu blank">-->
<!--            <li><router-link class="link_name" to="/chatroom">聊天室</router-link></li>-->
<!--          </ul>-->
        </li>
        <li>
          <div class="iocn-link">
            <router-link to="/game-server">
              <i class='bx bxl-steam'></i>
              <span class="link_name">游戏</span>
            </router-link>
            <i class='bx bxs-chevron-down arrow' ref="sidebarArrow"></i>
          </div>
          <ul class="sub-menu">
            <!--              <li><a class="link_name" href="#">Category</a></li>-->
            <li><router-link to="/game-server/left-4-dead-2">Left 4 Dead 2</router-link></li>
            <li><router-link to="/game-server/dont-starve">Don't Starve</router-link></li>
          </ul>
        </li>
        <li>
          <div class="iocn-link">
            <a href="#">
              <i class='bx bx-book-heart'></i>
              <span class="link_name">文章</span>
            </a>
            <i class='bx bxs-chevron-down arrow' ref="sidebarArrow"></i>
          </div>
          <ul class="sub-menu">
            <li><a class="link_name" href="#">Posts</a></li>
            <li><a href="#">Computer Science</a></li>
            <li><a href="#">Musicology</a></li>
            <li><a href="#">Design</a></li>
          </ul>
        </li>
        <!--          <li>-->
        <!--            <a href="#">-->
        <!--              <i class='bx bx-pie-chart-alt-2'></i>-->
        <!--              <span class="link_name">Analytics</span>-->
        <!--            </a>-->
        <!--            <ul class="sub-menu blank">-->
        <!--              <li><a class="link_name" href="#">Analytics</a></li>-->
        <!--            </ul>-->
        <!--          </li>-->
        <li>
          <router-link to="/chatroom">
            <i class='bx bx-tv'></i>
            <span class="link_name">聊天室</span>
          </router-link>
<!--          <ul class="sub-menu blank">-->
<!--            <li><a class="link_name" href="#">Chart</a></li>-->
<!--          </ul>-->
        </li>
        <li>
          <a href="#">
            <i class='bx bx-heart'></i>
            <span class="link_name">Deep Learning</span>
          </a>
        </li>
        <!--          <li>-->
        <!--            <div class="iocn-link">-->
        <!--              <a href="#">-->
        <!--                <i class='bx bx-plug'></i>-->
        <!--                <span class="link_name">Plugins</span>-->
        <!--              </a>-->
        <!--              <i class='bx bxs-chevron-down arrow' ref="sidebarArrow"></i>-->
        <!--            </div>-->
        <!--            <ul class="sub-menu">-->
        <!--              <li><a class="link_name" href="#">Plugins</a></li>-->
        <!--              <li><a href="#">UI Face</a></li>-->
        <!--              <li><a href="#">Pigments</a></li>-->
        <!--              <li><a href="#">Box Icons</a></li>-->
        <!--            </ul>-->
        <!--          </li>-->
        <!--          <li>-->
        <!--            <a href="#">-->
        <!--              <i class='bx bx-compass'></i>-->
        <!--              <span class="link_name">Explore</span>-->
        <!--            </a>-->
        <!--            <ul class="sub-menu blank">-->
        <!--              <li><a class="link_name" href="#">Explore</a></li>-->
        <!--            </ul>-->
        <!--          </li>-->
        <li>
          <a href="#">
            <i class='bx bx-history'></i>
            <span class="link_name">更新日志</span>
          </a>
          <ul class="sub-menu blank">
            <li><a class="link_name" href="#">History</a></li>
          </ul>
        </li>
        <li>
          <a href="#">
            <i class='bx bx-cog'></i>
            <span class="link_name">设置</span>
          </a>
          <ul class="sub-menu blank">
            <li><a class="link_name" href="#">设置</a></li>
          </ul>
        </li>
        <div class="sidebar__login" ref="login" @click="emit('loginlogout')">
          <div class="sidebar__login__text">
            <div class="login__status">{{ isLoginSuccess ? '登出' : '登录' }}</div>
            <div class="job">SnowCrystal</div>
          </div>
          <span class='bx bx-log-in' v-if="!isLoginSuccess" ></span>
          <span class="bx bx-log-out" v-else ></span>
        </div>
      </ul>
    </div>
  </div>

</template>

<style scoped lang="scss">
.sidebar__relative__root {
  top: 0;
  left: 0;
  grid-column: v-bind(sidebarLayout);
  grid-auto-columns: min-content;
  grid-row: 1/ -1;
  z-index: 2;
  transition: all 0.5s ease;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  transition: all 0.5s ease;
  background: #11101d;
}

.sidebar.close {
  width: calc(100vw / 12);
}

.sidebar .logo-details {
  height: 60px;
  width: 100%;
  display: flex;
  align-items: center;
}

.sidebar .logo-details i {
  font-size: 30px;
  color: #fff;
  height: 50px;
  min-width: calc(40px + 2vw);
  text-align: center;
  line-height: 50px;
}

.sidebar .logo-details .project__name {
  font-size: 22px;
  color: #fff;
  font-weight: 600;
  transition: 0.3s ease;
  transition-delay: 0.1s;
}

.sidebar.close .logo-details .project__name {
  transition-delay: 0s;
  opacity: 0;
  pointer-events: none;
}

.sidebar .nav-links {
  height: 100%;
  padding: 30px 0 150px 0;
  overflow: auto;
}

.sidebar.close .nav-links {
  overflow: visible;
}

.sidebar .nav-links::-webkit-scrollbar {
  display: none;
}

.sidebar .nav-links li {
  position: relative;
  list-style: none;
  transition: all 0.8s ease-in-out;
}

.sidebar .nav-links li:hover {
  background: #1d1b31;
}

.sidebar .nav-links li .iocn-link {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.sidebar.close .nav-links li{
  height: 64px;
}
.sidebar.close .nav-links li .iocn-link {
  display: block
}

.sidebar .nav-links li i {
  height: 50px;
  min-width: calc(40px + 2vw);
  text-align: center;
  line-height: 50px;
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.sidebar .nav-links li.showMenu i.arrow {
  transform: rotate(-180deg);
}

.sidebar.close .nav-links i.arrow {
  display: none;
}

.sidebar .nav-links li a {
  display: flex;
  align-items: center;
  text-decoration: none;
  height: 100%;
}

.sidebar .nav-links li a .link_name {
  font-size: 18px;
  font-weight: 400;
  color: #fff;
  transition: all 0.4s ease;
}

.sidebar.close .nav-links li a .link_name {
  opacity: 0;
  pointer-events: none;
}

.sidebar .nav-links li .sub-menu {
  padding: 6px 6px 14px 80px;
  margin-top: -10px;
  background: #1d1b31;
  display: none;
}

.sidebar .nav-links li.showMenu .sub-menu {
  display: block;
}

.sidebar .nav-links li .sub-menu a {
  color: #fff;
  font-size: 15px;
  padding: 5px 0;
  white-space: nowrap;
  opacity: 0.6;
  transition: all 0.3s ease;
}

.sidebar .nav-links li .sub-menu a:hover {
  opacity: 1;
}

.sidebar.close .nav-links li .sub-menu {
  position: absolute;
  left: 100%;
  top: -10px;
  margin-top: 0;
  padding: 10px 20px;
  border-radius: 0 6px 6px 0;
  opacity: 0;
  display: block;
  pointer-events: none;
  transition: 0s;
}

.sidebar.close .nav-links li:hover .sub-menu {
  top: 0;
  opacity: 1;
  pointer-events: auto;
  transition: all 0.4s ease;
}

.sidebar .nav-links li .sub-menu .link_name {
  display: none;
}

.sidebar.close .nav-links li .sub-menu .link_name {
  font-size: 18px;
  opacity: 1;
  display: block;
}

.sidebar .nav-links li .sub-menu.blank {
  opacity: 1;
  pointer-events: auto;
  padding: 3px 20px 6px 16px;
  opacity: 0;
  pointer-events: none;
}

.sidebar .nav-links li:hover .sub-menu.blank {
  top: 50%;
  transform: translateY(-50%);
}

.sidebar .sidebar__login {
  //position: fixed;
  bottom: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #1d1b31;
  padding: 6px 0;
  transition: all 0.5s ease;
  cursor: pointer;
}

.sidebar.close .sidebar__login {
  background: none;
  height: 50px;
}

.sidebar.close .sidebar__login {

}

.sidebar .sidebar__login .profile-content {
  display: flex;
  place-content: center;
  //align-items: center;
}

.sidebar .sidebar__login img {
  height: 52px;
  width: 52px;
  object-fit: cover;
  border-radius: 16px;
  margin: 0 14px 0 12px;
  background: #1d1b31;
  transition: all 0.5s ease;
}

.sidebar.close .sidebar__login img {
  padding: 10px;
}

.sidebar .sidebar__login .login__status,
.sidebar .sidebar__login .job {
  color: #fff;
  font-size: 18px;
  font-weight: 500;
  white-space: nowrap;
}

.sidebar.close .sidebar__login i,
.sidebar.close .sidebar__login .login__status,
.sidebar.close .sidebar__login .job {
  display: none;
}

.sidebar .sidebar__login .job {
  font-size: 12px;
}

.home-section {
  position: relative;
  background: #E4E9F7;
  height: 100vh;
  left: 260px;
  width: calc(100% - 260px);
  transition: all 0.5s ease;
}

.sidebar.close ~ .home-section {
  left: 78px;
  width: calc(100% - 78px);
}

.home-section .home-content {
  height: 60px;
  display: flex;
  align-items: center;
}

.home-section .home-content .bx-menu,
.home-section .home-content .text {
  color: #11101d;
  font-size: 35px;
}

.home-section .home-content .bx-menu {
  margin: 0 15px;
  cursor: pointer;
}

.home-section .home-content .text {
  font-size: 26px;
  font-weight: 600;
}
</style>